package com.tensquare.article.controller;

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommentService;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private StringRedisTemplate hashRedisTemplate;

    /**
     * 根据ID查评论
     *
     * @param id 评论ID
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable String id) {
        Comment comment = commentService.findById(id);
        return new Result(true, "查询成功", StatusCode.OK, comment);
    }

    /**
     * 查询所有评论
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        List<Comment> comments = commentService.findAll();
        return new Result(true, "查询成功", StatusCode.OK, comments);
    }

    /**
     * 新增评论
     *
     * @param comment 评论信息
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result saveComment(@RequestBody Comment comment) {
        commentService.save(comment);
        return new Result(true, "新增成功", StatusCode.OK);
    }

    /**
     * 删除评论
     *
     * @param id 评论ID
     */
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
        commentService.deleteById(id);
        return new Result(true, "删除成功", StatusCode.OK);
    }

    /**
     * 更新评论
     */
    @RequestMapping(method = RequestMethod.PUT)
    public Result update(@RequestBody Comment comment) {
        commentService.update(comment);
        return new Result(true, "更新成功", StatusCode.OK);
    }

    /**
     * 查询文章对应的评论
     */
    @RequestMapping(value = "article/{aid}", method = RequestMethod.GET)
    public Result findByArticleId(@PathVariable String aid) {
        List<Comment> comments = commentService.findByArticleId(aid);
        return new Result(true, "查询成功", StatusCode.OK, comments);
    }

    /**
     * 评论点赞
     *
     * @param id 评论Id
     */
    @RequestMapping(value = "thumbUp/{id}/{userId}", method = RequestMethod.PUT)
    public Result thumbUp(@PathVariable String id, @PathVariable String userId) {
//        String result = strRedisTemplate.opsForValue().get("thumbup_comment_" + userId + "_" + id);

        String resultValue = (String) hashRedisTemplate.opsForHash().get("thumbup_comment", id);
        String value = "";
        if (null == resultValue) {
            value = userId;
        } else {
            value = resultValue + "#" + userId;
            String[] split = resultValue.split("#");
            if (Arrays.asList(split).contains(userId)) {
                return new Result(false, "不能重复点赞", StatusCode.REPERROR);
            }
        }

        commentService.thumbUp(id, userId);
        //把点赞用户存入redis
//        strRedisTemplate.opsForValue().set("thumbu4p_" + userId + "_" + id, "1");
        hashRedisTemplate.opsForHash().put("thumbup_comment", id, value);
        return new Result(true, "点赞成功", StatusCode.OK);
    }
}
